package com.miniso.robot.feign;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.miniso.robot.bean.Message;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author guojunwang
 * @date 2021-10-18 20:30
 */
@Component
@FeignClient(name = "robotWebHookFeign",url = "${robot.webhook.url}",configuration = RobotFeignConfig.class)
public interface RobotFeign {

    @PostMapping("/send")
    public String sendMessage(@RequestParam(name="key") String robotKey, @RequestBody Message message);

    @PostMapping("/send")
    public void sendMessage(@RequestParam(name="key") String robotKey, @RequestBody String body);

    @RequestMapping(value = "/upload_media", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadFile(@RequestParam(name="key") String robotKey,@RequestParam(name="type")String type,@RequestPart("file") MultipartFile file);

    /**
     * 上传文件到机器人，返回文件id
     * @param robotKey
     * @param file
     * @return
     */
    default String uploadFile(String robotKey,MultipartFile file){

        JSONObject jsonObject = JSONUtil.parseObj(uploadFile(robotKey, "file", file));

        if( 0 ==jsonObject.get("errcode",Integer.class)){
            return jsonObject.get("media_id",String.class);
        }
        return null;
    }
}
